package com.yunlongn.async.scheduler;

import com.yunlongn.async.content.NameThreadFactory;
import com.yunlongn.async.service.RetrySchedulerService;
import com.yunlongn.async.service.impl.RetryScheduleServiceImpl;
import com.yunlongn.async.service.impl.SimpRetryScheduleServiceImpl;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 重试调度器
 * @author yunlongn
 * @since 20210907
 */
public class RetryScheduler {
    /**
     * 创建ScheduleService
     * @return
     */
    public static RetrySchedulerService createScheduleService() {
        return new RetryScheduleServiceImpl(
                createExecutorService(3),1000);
    }

    /**
     * 创建固定线程数的线程池
     * @param threads 线程数
     */
    private static ExecutorService createExecutorService(final int threads) {
        int nThreads = Math.max(Runtime.getRuntime().availableProcessors(), threads);
        return new ThreadPoolExecutor(nThreads, nThreads, 60L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new NameThreadFactory("scheduler-async-", true));
    }


}
